<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="header-container">
      <div class="search-toggle-area">
        <el-button class="search-toggle-button" @click="toggleSearchBar">
          <el-icon><Search /></el-icon>
        </el-button>
        <el-form :inline="true" :model="searchForm" class="search-form" :class="{'is-hidden': !showSearchBar}">
          <el-form-item label="楼宇名称">
            <el-input v-model="searchForm.name" placeholder="请输入楼宇名称" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 卡片区域 -->
    <el-row :gutter="20" class="card-container" v-loading="loading">
      <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="building in buildingList" :key="building.building_id">
        <el-card class="building-card" shadow="hover" @click="viewDetails(building)" :style="{'--building-border-color': getBuildingBorderColor(building.building_type)}">
          <template #header>
            <div class="card-header">
              <el-icon><OfficeBuilding /></el-icon>
              <span class="building-name">{{ building.name }}</span>
            </div>
          </template>
          <div class="card-body">
            <p><strong>类型:</strong> 
              <el-tag size="small" effect="dark" :type="getBuildingTypeTag(building.building_type)">
                {{ building.building_type }}
              </el-tag>
            </p>
            <p><strong>总楼层:</strong> {{ building.total_floors }} 层</p>
            <p><strong>建筑面积:</strong> {{ building.building_area }} ㎡</p>
            <p><strong>建成日期:</strong> {{ building.completion_date }}</p>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <div v-if="!loading && buildingList.length === 0" class="empty-state">
      <el-empty description="暂无楼宇数据"></el-empty>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getBuildingList } from '@/api/assets.js'
import { OfficeBuilding, Search } from '@element-plus/icons-vue'
import { ref } from 'vue'

export default {
  name: 'BuildingList',
  components: {
    OfficeBuilding,
    Search
  },
  data() {
    return {
      loading: false,
      searchForm: {
        name: '',
      },
      buildingList: [], // 楼宇列表数据
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      showSearchBar: false, // 控制搜索栏显示状态
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取楼宇列表
    async fetchData() {
      this.loading = true
      try {
        const params = {
          current: this.page.current,
          size: this.page.size,
          ...this.searchForm
        }
        const response = await getBuildingList(params)
        if (response.data.code === 200) {
          this.buildingList = response.data.data.list
          this.page.total = response.data.data.total
        }
      } catch (error) {
        this.$message.error('获取楼宇列表失败')
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    // 切换搜索栏显示状态
    toggleSearchBar() {
      this.showSearchBar = !this.showSearchBar
    },
    // 搜索
    onSearch() {
      this.page.current = 1
      this.fetchData()
    },
    // 重置搜索表单
    resetForm() {
      this.searchForm = {
        name: '',
      }
      this.onSearch()
    },
    // 处理每页显示数量变化
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    // 处理页码变化
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    },
    // 查看楼宇详情
    viewDetails(row) {
      this.$router.push(`/admin/assets/BuildingDetail/${row.building_id}`)
    },
    // 获取楼宇类型对应的标签类型
    getBuildingTypeTag(type) {
      switch(type) {
        case '研发楼': return 'primary';
        case '办公楼': return 'success';
        case '生产厂房': return 'warning';
        case '仓库': return 'danger';
        case '人才公寓': return 'info';
        case '综合服务中心': return '';
        default: return 'info';
      }
    },
    // 获取楼宇类型对应的边框颜色
    getBuildingBorderColor(type) {
      switch(type) {
        case '研发楼': return '#409EFF';
        case '办公楼': return '#67C23A';
        case '生产厂房': return '#E6A23C';
        case '仓库': return '#F56C6C';
        case '人才公寓': return '#909399';
        case '综合服务中心': return '#c772e0';
        default: return '#b1b3b8';
      }
    },
  }
}
</script>

<style scoped>
/* 基础布局 */
.building-container {
  padding: 20px;
  background-color: #f8f9fa;
  min-height: calc(100vh - 80px);
}

/* 头部布局 */
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding: 10px 20px;
  background-color: transparent;
  border-radius: 10px;
  position: relative;
}

/* 搜索区域 */
.search-toggle-area {
  display: flex;
  flex-direction: row;
  align-items: center;
  flex-grow: 1;
}

.search-toggle-button {
  font-size: 20px;
  color: #606266;
  align-self: center;
  margin-right: 15px;
  transition: all 0.2s;
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.03);
  border: 1px solid rgba(0, 0, 0, 0.05);
  z-index: 10;
  margin-bottom: 0;
}

.search-toggle-button:hover {
  color: #409eff;
  background-color: rgba(64, 158, 255, 0.1);
  border-color: rgba(64, 158, 255, 0.2);
}

.search-form {
  max-width: 1000px;
  width: auto;
  overflow: hidden;
  transition: max-width 0.4s ease-in-out, opacity 0.4s ease-in-out, background-color 0.4s ease-in-out, box-shadow 0.4s ease-in-out, padding 0.4s ease-in-out, margin-left 0.4s ease-in-out, height 0.4s ease-in-out, min-height 0.4s ease-in-out;
  opacity: 1;
  background-color: transparent;
  border-radius: 8px;
  padding: 0 20px;
  box-shadow: none;
  pointer-events: auto;
  margin-left: 15px;
  white-space: nowrap;
  height: 35px;
  min-height: 35px;
}

.search-form.is-hidden {
  max-width: 0;
  opacity: 0;
  padding: 0;
  margin-left: 0;
  height: 0;
  min-height: 0;
  background-color: transparent;
  box-shadow: none;
  pointer-events: none;
}

/* 调整搜索表单项的底部外边距 */
:deep(.search-form .el-form-item) {
  margin-bottom: 0;
  margin-right: 15px;
  display: inline-flex;
  align-items: center;
}

:deep(.search-form .el-form-item__label) {
  padding-right: 8px;
}

/* 卡片区域 */
.card-container {
  min-height: 400px;
}

.building-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 4px solid var(--building-border-color, #409EFF);
}

.building-card:hover {
  box-shadow: 0 8px 16px rgba(0,0,0,0.1);
  transform: translateY(-2px);
  border-color: var(--building-border-color);
}

.card-header {
  display: flex;
  align-items: center;
  font-weight: bold;
}

.building-name {
  margin-left: 8px;
  font-size: 16px;
}

.card-body p {
  margin: 0 0 10px 0;
  font-size: 14px;
  color: #606266;
  display: flex;
  align-items: center;
}

.card-body p:last-child {
  margin-bottom: 0;
}

.empty-state {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 40px;
}

/* 分页区域 */
.pagination-container {
  margin-top: 30px;
  display: flex;
  justify-content: flex-end;
  padding: 15px 20px;
  background-color: #f9fbfd;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}

/* 输入框样式优化 */
:deep(.el-input__inner) {
  border-color: #e0e5ed !important;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-input__inner:focus) {
  border-color: rgba(64, 158, 255, 0.4) !important;
}

:deep(.el-input__inner::placeholder) {
  color: #aebccf;
}

/* 按钮样式优化 */
:deep(.el-button--primary) {
  background-color: rgba(64, 158, 255, 0.1) !important;
  color: #606266 !important;
  border-color: rgba(64, 158, 255, 0.2) !important;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-button--primary:hover) {
  background-color: rgba(64, 158, 255, 0.15) !important;
  border-color: rgba(64, 158, 255, 0.3) !important;
}

:deep(.el-button--default) {
  background-color: #ffffff !important;
  color: #606266 !important;
  border-color: #dcdfe6 !important;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-button--default:hover) {
  background-color: #f7f9fa !important;
  border-color: #c8d3dd !important;
}
</style> 